﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MimiDouban.Abstract;
using System.Timers;
using System.Windows.Threading;
using System.Windows.Media.Animation;

namespace MimiDouban.UI
{
    /// <summary>
    /// 用于显示右下角文字的类
    /// </summary>
    public partial class TipsOutputUI : Window,IOutputUI
    {
        //主窗口引用，只有当主窗口为最小化时，才显示内容
        private Window mainWindow;
        private Timer timer;
        private delegate void hideMeDelType();
        private hideMeDelType hideMeDel;

        private Storyboard story;

        public TipsOutputUI(Window mainWindow)
        {
            InitializeComponent();
            this.mainWindow = mainWindow;
        }

        #region IOutputUI Members

        public void start()
        {
            //throw new NotImplementedException();
        }

        public void ShowSongInfo(MimiDouban.Abstract.Beans.DoubanSong song)
        {
            //throw new NotImplementedException();
        }

        public void ShowChannel(MimiDouban.Abstract.Beans.DoubanChannel channel)
        {
            //throw new NotImplementedException();
        }

        public void ShowNextSong(MimiDouban.Abstract.Beans.DoubanSong song)
        {
            if (mainWindow.WindowState == WindowState.Minimized)
            {
                txtMessage.Text = "下一首：" + song.Title;
                try
                {
                	showMe();
                }
                catch (System.Exception ex)
                {
                    MimiDouban.Test.TestLog.getInstance().error("tips ShowNextSong catch:", ex);
                }
            }
        }                 

        public void showTime(int min, int sec)
        {
        }
        public void showLyric(string str, int currentTime,TimeSpan dur)
        {
        }
        public void showBuffer(string str,int currentTime)
        {
            if ( this!=null )
            {
                //每首歌初始状态为buffering，如果不设定大于10秒，则每首歌开始播放时都会显示正在缓冲，而不是歌名
                if ( currentTime > 10 && mainWindow.WindowState == WindowState.Minimized && str!="")
                {
                    txtMessage.Text = str;
                    try
                    {
                        showMe();
                    }
                    catch (System.Exception ex)
                    {
                        MimiDouban.Test.TestLog.getInstance().error("tips showBuffer catch:", ex);
                    }
                }
            }            
        }

        public void showLoginning()
        {
            
        }

        public void showVolume(double volume)
        {
        }

        public void setLikeStatus(ControlStatus status)
        {
            if (mainWindow.WindowState == WindowState.Minimized)
            {
                txtMessage.Text = (status == ControlStatus.Pressed ? "我喜欢" : "取消我喜欢");
                try
                {
                    showMe();
                }
                catch (System.Exception ex)
                {
                    MimiDouban.Test.TestLog.getInstance().error("tips setLikeStatus catch:", ex);
                }
            }
        }

        public ControlStatus getLikeStatus()
        {
            //throw new NotImplementedException();
            return ControlStatus.Enabled;
        }

        public void setDislikeStatus(ControlStatus status)
        {
            //throw new NotImplementedException();
        }

        public void setPauseStatus(bool status)
        {
        }

        public void showLogin()
        {
            //throw new NotImplementedException();
        }

        public void showLoginCodeImage(string loginCode)
        {

        }

        public MimiDouban.Abstract.Beans.DoubanChannel[] Channels
        {
            get
            {
                //throw new NotImplementedException();
                return null;
            }
            set
            {
                //throw new NotImplementedException();
            }
        }

        public void end()
        {
            this.Close();
        }


        public void showCurrentUser(string username)
        {
            //throw new NotImplementedException();
        }

        public void showLoginBtn()
        {
            //throw new NotImplementedException();
        }

        #endregion

        private void showMe()
        {
            double screenHeight = SystemParameters.PrimaryScreenHeight;
            double screenWidth = SystemParameters.PrimaryScreenWidth;
            this.Top = screenHeight - this.Height - 50;
            this.Left = screenWidth - this.Width - 50;

            //先把上一次动画停下来
            if (story != null && story.GetCurrentState() == ClockState.Active)
            {
                story.Stop();
            }

            //开始动画
            this.Show();
            story = (Resources["ShowText"] as Storyboard);
            story.Begin();
        }

        private void ShowText_Completed(object sender, EventArgs e)
        {
            this.txtMessage.Text = "";
            this.Hide();
        }     
    }
}
